package com.bltec.emergency.controller.recordController;


import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.RecordExercise;
import com.bltec.emergency.service.recordService.RecordExerciseService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/exercise")
@RequiresPermissions("project:record")
public class ExerciseController {
    @Resource
    private RecordExerciseService recordExerciseService;
    @Resource
    private UserService userService;
    /**
     * 获得所有exercise
     */
    @RequestMapping("getAllExercise")
    public ResponseJson getUser() {
        try {
            List<RecordExercise> recordExercise = recordExerciseService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(recordExercise));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, "获取所有exercise失败:"+e.getMessage());
        }
    }

    /**
     * 添加exercise
     * id,exerciseName,exerciseType,occuredPosition,issueTime,casualties,href
     */
    @RequestMapping("addExercise")
    @RequiresPermissions("record:insert")
    public ResponseJson addUser(Date exerciseTime, String exerciseRequirement, String exercisePosition, String exerciseName, String exerciseContent, String exerciseUnit) {
        try {
            RecordExercise exercise = new RecordExercise();
            exercise.setExerciseTime(exerciseTime);
            exercise.setExerciseRequirement(exerciseRequirement);
            exercise.setExercisePosition(exercisePosition);
            exercise.setExerciseName(exerciseName);
            exercise.setExerciseContent(exerciseContent);
            exercise.setExerciseUnit(exerciseUnit);
            exercise.setEnable(true);
            exercise.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            exercise.setCreateTime(DateUtil.dateNow());
            recordExerciseService.add(exercise);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败:" + e.getMessage());
        }
    }

    /**
     * 修改exercise
     */
    @RequiresPermissions("record:update")
    @RequestMapping("updateExercise")
    public ResponseJson updateUser(Long id, Date exerciseTime, String exerciseRequirement, String exercisePosition, String exerciseName, String exerciseContent, String exerciseUnit) {
        try {
            RecordExercise exercise = recordExerciseService.getExerciseById(id);
            exercise.setExerciseTime(exerciseTime);
            exercise.setExerciseRequirement(exerciseRequirement);
            exercise.setExercisePosition(exercisePosition);
            exercise.setExerciseName(exerciseName);
            exercise.setExerciseContent(exerciseContent);
            exercise.setExerciseUnit(exerciseUnit);
            exercise.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            exercise.setUpdateTime(DateUtil.dateNow());
            recordExerciseService.updateExerciseById(exercise);
            return new ResponseJson(Constant.OK, JSON.toJSON(exercise));
        } catch (Exception e) {

            return new ResponseJson(Constant.UPDATE_ERROR,"更新失败:"+e.getMessage());
        }
    }

    /**
     * 删除exercise
     */
    @RequiresPermissions("record:delete")
    @RequestMapping("deleteExercise")
    public ResponseJson setDisable(Long id) {
        if (null != recordExerciseService.getExerciseById(id)) {
            if (recordExerciseService.deleteExerciseById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }


}
